<?php

try
{
	require_once 'init.inc';
	require_once 'cf/image.inc';

	$groupId = $par->get('groupId');
	addGroupToPath($groupId);
	$group = new Group($groupId);
	
	$name = $group->name();
	
	if ($group->pageTitle()) {
		$title = $group->pageTitle();
	} else {
		if ($group->isChildOf('boats'))	{
			$title = "надувные лодки пвх – " . $group->name() . " – магазин Лодки Моторы Палатки";
		} elseif ($group->isChildOf('motors')) {
			$title = "Лодочные моторы – " . $group->name() . " – магазин Лодки Моторы Палатки";
		} elseif ($group->isChildOf('electromotors')) {
			$title = "Электромоторы – " . $group->name() . " – магазин Лодки Моторы Палатки";
		} else {
			$title = $group->name() . " - Лодки Моторы Палатки";
		}
	}
	
	$descendantsOb = new DescendantGroups($groupId);
	$descendants = Groups::toHierarchyTree($groupId);
	foreach ($descendants as &$d) {
		$d['depth'] = $d['depth'] - $group->nestingDepth();
	}
	
	
	$products = array();
	$productImages = array();
	$newProducts = array();
	$manufacturers = array();
	$prices = array();
	$newPrices = array();

	function getFirstDescendantWithProducts($parentId)
	{
		$descendants = new DescendantGroups($parentId);
		foreach ($descendants as $group) 
		{
			if ($group->hasProducts()) {
				return $group->id();
			}
		}
		return $parentId;
	}



	function getFirstNot0Price($productId)
	{
		$product = new Product($productId);
		if ($product->hasPrice()) 
		{
			return array(0, $product->price());
		}
		
		$options = new ProductOptions($productId);
		foreach ($options as $option) 
		{
			if ($option->hasPrice()) 
			{
				return array($option->id(), $option->price());
			}
		}
		
		return false;
	}

	$keywords = '';
	$productsOb = new ProductsInSubtree($groupId);
	$products = $productsOb->getList();
	foreach ($products as $product){
		$manufacturer = new Manufacturer($product->manufacturerId());
		$manufacturers[] = $manufacturer;
	
		if (count($product->images())) {
			$images = $product->images();
			$productImages[] = $images[0];
		}
		else {
			$productImages[] = null;
		}
	
		$newProducts[] = $product->isInGroup('new');
	
		if ($keywords != '') {
			$keywords .= ',';
		}
		$keywords .= $product->name().' '.$manufacturer->name().' '.$product->model();
	}
	
	if ($group->pageDescr()) {
		$description = $group->pageDescr();
	} else {
		$description = $keywords;
	}
	
	if ($group->pageKeywords()) {
		$keywords = $group->pageKeywords();
	}
	
/*
	if ($group->hasProducts() || $group->descr()=='')	//show some products
	{
		$productsGroupId = 
			$group->hasProducts() ? 
				$groupId : 
				getFirstDescendantWithProducts($groupId);
				
				
		$recCount = countRecords('cf_products_in_groups', array('group_id'=>$productsGroupId));
		if ($recCount) 
		{
			define('RECS_ON_PAGE', 30);
			define('PAGES_IN_LIST', 20);

			$ruler = new PageFormatter($recCount, RECS_ON_PAGE, PAGES_IN_LIST);
			$ruler->create();
			
			$q = createQuery("
				SELECT product_id 
				FROM cf_products_in_groups
				WHERE group_id=$productsGroupId
				ORDER BY sort_order ASC
				LIMIT {$ruler->getStartRec()}, {$ruler->getRecOnPage()}
			");
			$q->execute();
			
			while ($q->fetch()) 
			{
				$productId = $q->at(0);
				
				$product = new Product($productId);
				$products[] = $product;

				$manufacturer = new Manufacturer($product->manufacturerId());
				$manufacturers[] = $manufacturer;
				
				if (count($product->images())) 
				{
					$images = $product->images();
					$productImages[] = $images[0];
				}
				else {
					$productImages[] = null;
				}
				
				$newProducts[] = $product->isInGroup('new');
				
				if ($keywords != '') {
					$keywords .= ',';
				}
				$keywords .= $product->name() . ' ' . $manufacturer->name() . ' ' . $product->model();
			}

			$q->close();
		
			$pageNumbers = $ruler->visiblePages();
			assignTemplateVars('pageNumbers','ruler');
		}
	}
*/

	$url = 'group.php?groupId=' . $group->id();
	
	function getImage($prod,$width,$height) {
		$images = $prod->images();
		$thumb = new Thumbnail($images[0], $width, $height);
		return $thumb->toArray();
	}
	
	if (Group::toID('boatandmotor')==$groupId) {
		$boatsMotors = array();
		foreach ($products as $p) {
			$ids = explode('+',trim(strip_tags($p->fullDescr())));
			$boat = new Product($ids[0]);
			$motor = new Product($ids[1]);
			$boatsMotors[] = array(
				'boat'=>array('image'=>getImage($boat,120,150)),
				'motor'=>array('image'=>getImage($motor,120,150))
			);
		}
		assignTemplateVars('boatsMotors');
	}

	assignTemplateVars(
		'name',
		'title',
		'description',
		'keywords',
		'descendants',
		'group',
		'products',
		'manufacturers',
		'productImages',
		'newProducts',
		'url'
	);

	display('group');
}
catch(Exception $e) {
	header("Location: error.php");
}
?>